#include "Logger.h"

QFile Logger::logFile;
QMutex Logger::mutex;

void Logger::init(const QString& fileName) {
    QMutexLocker locker(&mutex);
    logFile.setFileName(fileName);
    if (!logFile.open(QIODevice::Append | QIODevice::Text)) {
        ELOG("初始化Logger对象失败");
    }
}

void Logger::log(const QString& level, const QString& message, const char* file, int line) {
    QMutexLocker locker(&mutex);
    if (logFile.isOpen()) {
        QTextStream out(&logFile);
        QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
        out << "[" << level << "]" << timestamp << " [" << file << ":" << line << "] " << message << "\n";
        out.flush();
    }
}
